package com.keta.foundation.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.Range;

@Entity
@Table(name = "t_organization")
public class Organization implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2206006906951381769L;

	/**
	 * 组织ID
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	/**
	 * 组织名称
	 */
	@NotBlank
	@Length(max = 64)
	@Column(length = 64, nullable = false)
	private String name;

	/**
	 * 优先级，越小优先级越高
	 */
	@NotNull
	@Range(min = 1, max = 999)
	@Column(length = 3, nullable = false)
	private Integer priority = 999;

	/**
	 * 组织简介
	 */
	@Length(max = 256)
	@Column(length = 256)
	private String description;

	/**
	 * 父组织
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "parentId")
	private Organization parent;

	/**
	 * 子组织
	 */
	@OneToMany(mappedBy = "parent")
	@OrderBy("priority ASC")
	private List<Organization> children = new ArrayList<Organization>();
	
	@OneToMany(fetch=FetchType.LAZY,mappedBy="organization")
	private List<OrganizationRole> organizationRoles ;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getPriority() {
		return priority;
	}

	public void setPriority(Integer priority) {
		this.priority = priority;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Organization getParent() {
		return parent;
	}

	public void setParent(Organization parent) {
		this.parent = parent;
	}

	public List<Organization> getChildren() {
		return children;
	}

	public void setChildren(List<Organization> children) {
		this.children = children;
	}
	
	
	public List<OrganizationRole> getOrganizationRoles() {
		return organizationRoles;
	}

	public void setOrganizationRoles(List<OrganizationRole> organizationRoles) {
		this.organizationRoles = organizationRoles;
	}

	public List<Role> getRoles() {
		List<Role> roles = new ArrayList<>();
		List<OrganizationRole> organizationRoles = getOrganizationRoles();
		for (OrganizationRole orgRole : organizationRoles) {
			roles.add(orgRole.getRole());
		}
		return roles;
	}
	
	/*
	 * 当前组织的所拥有的角色包括 当前角色 和 所有上级组织的拥有的角色
	 */
	public List<Role> getLogicRoles() {
		List<Role> roles = new ArrayList<>();
		List<Role> curroles = this.getRoles();
		roles.addAll(curroles); 
		Organization _parent = this.getParent();
		//使用lazy加载不能使用parent!=null判断，因为parent为一个代理对象
		if(parent instanceof Organization){
			roles.addAll(_parent.getLogicRoles());
		}
		return roles;
	}
}
